//throw exception in destructor
//terminate() will be called! because it can't catch more than one exception
//at a time
#include <iostream>
#include <exception>
using namespace std;
class Myexception : exception{
public:
  const char* what() const throw() { return "Myexception";}
};

class A{
public:
  A(){cout<<"A()"<<endl;}
  ~A();
};

A::~A()
{
  cout<<"~A()"<<endl;
  if(!uncaught_exception()){
    throw Myexception();
  }
}

  
void foo()
{
  A a;
  try {
    cout<<"foo()::try"<<endl;
    throw exception();
  }
  catch(...) {
    cout<<"foo()::catch"<<endl;
    throw;
  }
  cout<<"after try-catch"<<endl;
  return;
}

int main()
{
  try {
    foo();
  }
  catch (exception& e) {
    cout<<"Caught Exception"<<e.what()<<endl;
  }
  return 0;
}
